package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.query.CustomerChartQueryObject;
import cn.wolfcode.rbac.service.ICustomerChartService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerChart")
public class CustomerChartController {
   @Autowired
    private ICustomerChartService service;
   @RequestMapping("list")
   public String list(Model model, @ModelAttribute("qo") CustomerChartQueryObject qo) {
       List<Map<String, Object>> maps = service.selectChart(qo);
       model.addAttribute("result",maps);
       return "customerChart/list";
   }
   @RequestMapping("chartByBar")
   public String chartByBar(Model model, @ModelAttribute("qo") CustomerChartQueryObject qo) {
       List<Map<String, Object>> maps = service.selectChart(qo);
       List<Object> xList=new ArrayList<>();
       List<Object> yList=new ArrayList<>();
       for(Map<String, Object> map:maps){
           xList.add(map.get("groupType"));
           yList.add(map.get("number"));
       }
       model.addAttribute("xList", JSON.toJSONString(xList));
       model.addAttribute("yList",JSON.toJSONString(yList));
       return "customerChart/chartByBar";
   }
   @RequestMapping("chartByPie")
   public String chartByPie(Model model, @ModelAttribute("qo") CustomerChartQueryObject qo) {
       List<Map<String, Object>> maps = service.selectChart(qo);
       List<Object> xList=new ArrayList<>();
       List<Object> data=new ArrayList<>();
       for(Map<String, Object> map:maps){
           xList.add(map.get("groupType"));
           HashMap<String,Object> temp=new HashMap<>();
           temp.put("name",map.get("groupType"));
           temp.put("value",map.get("number"));
           data.add(temp);
       }
       model.addAttribute("xList", JSON.toJSONString(xList));
       model.addAttribute("data",JSON.toJSONString(data));
       return "customerChart/chartByPie";
   }
}
